<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Ratings
 *
 * @author miroslav
 */
include_once dirname(__FILE__) . '/db.php';

class ratings {

    private $widget_id;
    private $data = array();
    private $conn;

    function __construct($wid) {
        //$wid = substr($wid, 1);
        $this->widget_id = $wid;
        $this->hostelID = $hostel;
        //$this->conn = dbConnect("sib");

        $this->data["widget_id"] = $wid;
        $this->data["number_votes"] = 0;
        $this->data["total_points"] = 0;
        $this->data["dec_avg"] = 0;
        $this->data["whole_avg"] = 0;
    }

    function __destruct() {
        mysql_close($this->conn);
    }

    public function get_ratings_old() {
        if ($this->data[$this->widget_id]) {
            echo json_encode($this->data[$this->widget_id]);
        } else {
            $data['widget_id'] = $this->widget_id;
            $data['number_votes'] = 0;
            $data['total_points'] = 0;
            $data['dec_avg'] = 0;
            $data['whole_avg'] = 0;
            echo json_encode($data);
        }
    }

    public function vote_old() {
        preg_match('/star_([1-5]{1})/', $_POST['clicked_on'], $match);
        $vote = $match[1];


        $ID = $this->widget_id;
        if ($this->data[$ID]) {
            $this->data[$ID]['number_votes'] += 1;
            $this->data[$ID]['total_points'] += $vote;
        } else {
            $this->data[$ID]['number_votes'] = 1;
            $this->data[$ID]['total_points'] = $vote;
        }

        $this->data[$ID]['dec_avg'] = round($this->data[$ID]['total_points'] / $this->data[$ID]['number_votes'], 1);
        $this->data[$ID]['whole_avg'] = round($this->data[$ID]['dec_avg']);

        file_put_contents($this->data_file, serialize($this->data));
        $this->get_rateings();
    }

    public function get_rateings() {
        //$ID = $this->widget_id;
        //$sql = "SELECT avg($ID) as result FROM feedback WHERE IDHostel = $this->hostelID;"; //write query
        //$result = mysql_query($sql, $this->conn) or die(mysql_error());
        //if ($result) {
        //$row = mysql_fetch_array($result);
//        } else {
//            $this->data["widget_id"] = $ID;
//            $this->data["number_votes"] = 0;
//            $this->data["total_points"] = 0;
//            $this->data["dec_avg"] = 0.0;
//            $this->data["whole_avg"] = 0;
//        }
        echo json_encode($this->data);
    }

    public function vote() {
        if ($this->wasVoted()) {
            preg_match('/star_([1-5]{1})/', $_POST['clicked_on'], $match);
            $vote = $match[1];

//            $ID = $this->widget_id;
//            $select_query = "SELECT RateNum, RateSum FROM rateing WHERE IDRate = '$ID';"; //vraca vrednosti koje treba povecati (broj glasanja i ukupan broj glasova)
//            $result = mysql_query($select_query, $this->conn);
//            $row = mysql_fetch_array($result);
            $this->data["number_votes"]=1;
            $this->data["total_points"]=$vote;
            $this->data["dec_avg"] = $vote;
            $this->data["whole_avg"] = $vote;
//            $rateNum = 1;
//            $rateSum = $vote;

//            $update_query = "UPDATE rateing SET RateNum = '$rateNum', RAteSum = '$rateSum' WHERE IDRate = '$ID'"; //povecava te vrednosti za 1 i upisuje nazad u bazu
//            mysql_query($update_query, $this->conn);

            $this->get_rateings();
        }
    }

    private function wasVoted() {
        //treba implementirati proveru da li je osoba vec glasala
        return true;
    }

    public function vote_nonDb() {
        preg_match('/star_([1-5]{1})/', $_POST['clicked_on'], $match);
        $vote = $match[1];

        $this->data["widget_id"] = $this->widget_id;
        $this->data["number_votes"] = 1;
        $this->data["total_points"] = $vote;
        $this->data["dec_avg"] = $vote . '.0';
        $this->data["whole_avg"] = $vote;

        echo json_encode($this->data);
    }

}

$rating = new ratings("security");
$rating->get_rateings();
//$rating->vote();
?>
